package kr.re.etri.snsenabler.client;

import java.util.ArrayList;
import java.util.logging.Logger;

import kr.re.etri.snsenabler.client.service.SnsEnablerService;
import kr.re.etri.snsenabler.client.service.SnsEnablerServiceAsync;
import kr.re.etri.snsenabler.shared.Data;
import kr.re.etri.snsenabler.shared.LoginProperties;
import kr.re.etri.snsenabler.shared.ServiceResponse;
import kr.re.etri.snsenabler.shared.SocialServiceProperties;
import kr.re.etri.snsenabler.shared.SocialServiceProvider;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.Style;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer.BorderLayoutData;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;

public class AuthoringTool {

	private static AuthoringUiBinder uiBinder = GWT.create(AuthoringUiBinder.class);

	interface AuthoringUiBinder extends UiBinder<Widget, AuthoringTool> {
	}

	private final SnsEnablerServiceAsync snsEnablerService = GWT.create(SnsEnablerService.class);
	@SuppressWarnings("unused") private final Logger logger = Logger.getLogger(getClass().getName());

	private final Data user;
	private BorderLayoutContainer container;
	@UiField ContentPanel westPanel;
	@UiField VerticalLayoutContainer socialServiceProvidersPanel;
	@UiField ContentPanel centerPanel;
	@UiField(provided = true) BorderLayoutData westData = new BorderLayoutData(250);
	@UiField(provided = true) MarginData centerData = new MarginData();

	public AuthoringTool(Data user) {
		super();

		this.user = user;

		westData.setMargins(new Margins(10));
		westData.setCollapsible(true);
		westData.setSplit(true);

		centerData.setMargins(new Margins(10, 10, 10, 0));

		container = (BorderLayoutContainer)uiBinder.createAndBindUi(this);
		container.setPixelSize(Window.getClientWidth(), Window.getClientHeight());

		loadSocialServiceProviders();
	}

	private void loadSocialServiceProviders() {
		snsEnablerService.loadJoinedSocialServiceProviders(user.getString(LoginProperties.LOGIN_ID), new AsyncCallback<ServiceResponse>() {
			public void onFailure(Throwable caught) {
				AlertMessageBox messageBox = new AlertMessageBox("Alert", "Invalid Authorization");
				messageBox.show();
			}

			public void onSuccess(ServiceResponse response) {
				if (response.isFailed()) {

				} else {
					socialServiceProvidersLoaded(response.getDataList());
				}
			}
		});
	}

	private void socialServiceProvidersLoaded(ArrayList<Data> dataList) {
		for (int i = 0; i < SocialServiceProvider.values().length; i++) {
			SocialServiceProvider socialServiceProvider = SocialServiceProvider.values()[i];
			boolean isAuthorized = false;
			String userId = null;
			for (int j = 0; j < dataList.size(); j++) {
				String providerName = dataList.get(j).getString(SocialServiceProperties.SERVICE_PROVIDER);
				SocialServiceProvider usingSocialServiceProvider = SocialServiceProvider.fromName(providerName);
				if (socialServiceProvider == usingSocialServiceProvider) {
					isAuthorized = true;
					userId = dataList.get(j).getString(SocialServiceProperties.USER_ID);
					break;
				}
			}

			ServiceProviderButton button = new ServiceProviderButton(user, userId, socialServiceProvider, isAuthorized);
			VerticalLayoutData data = new VerticalLayoutData(1, Style.DEFAULT, new Margins(10, 10, 0, 10));
			socialServiceProvidersPanel.add(button, data);
		}
	}

	protected SimpleContainer getContainer() {
		return container;
	}
}
